package com.cn.wanxi.servlet.back;

import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/check/username")
public class UsernameCheck extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //检查用户名是否已存在
        resp.setContentType("text/html; charset=utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        IUserService userService = new UserServiceImpl();
        boolean flag = userService.userIsExist(username);
        if (flag==false)//如果为空，则该用户名为未被占用，false
            resp.getWriter().print("false");
        else//如果不为空，则该用户名已被占用，true
            resp.getWriter().print("true");
    }
}
